//package com.gudaomai.ekproject.common;
//import java.io.FileDescriptor;
//import java.lang.reflect.Method;
//import java.net.InetAddress;
//import java.net.UnknownHostException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.UUID;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
//import org.apache.log4j.Logger;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.After;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//import org.springframework.validation.BeanPropertyBindingResult;
//import org.springframework.web.multipart.commons.CommonsMultipartFile;
//
//import com.cn.gongan.bean.ExUserBean;
//import com.cn.gongan.bean.LogBean;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//@Component
//@Aspect
//public class LogAop {
// 
//    ThreadLocal<Long> time=new ThreadLocal<Long>();
//    ThreadLocal<String> tag=new ThreadLocal<String>();
//    
//    private final Logger logger = Logger.getLogger("日志拦截");
//    
//    //配置接入点
//    @Pointcut("execution(* com.cn.gongan.rest.Ex*.*(..))")  
//    private void controllerAspect(){}//定义一个切入点
//     
//    @Around("controllerAspect()")
//    public Object aroundExec(ProceedingJoinPoint pjp) throws Throwable {
//
//        before(pjp);
//    	
//        Object obj =pjp.proceed();
//        after(pjp);
//        return obj;
//    }
//     
//    /**
//     * 
//     * @param pjp
//     * @throws JsonProcessingException 
//     */
//    private void before(JoinPoint pjp) throws JsonProcessingException{
//    	
//        time.set(System.currentTimeMillis());
//    	
//        LogBean  logBean = new LogBean(); 
//        List list = new ArrayList();
//        for (int i=0;i<pjp.getArgs().length;i++) {
//        	
//        	if (!(pjp.getArgs()[i] instanceof HttpServletRequest 
//        			|| pjp.getArgs()[i] instanceof HttpServletResponse 
//        			|| pjp.getArgs()[i] instanceof CommonsMultipartFile
//        			|| pjp.getArgs()[i] instanceof BeanPropertyBindingResult)) 
//        	{
//        		list.add(pjp.getArgs()[i]);
//        	}
//        	
//        }
//        logBean.setArgs(list);
//        MethodSignature ms=(MethodSignature) pjp.getSignature();
//        Method method=ms.getMethod();
//        
//        if (method.isAnnotationPresent(Log.class)) {
//        	 String log = method.getAnnotation(Log.class).value();
//             logBean.setContent(log);
//        }
//
//        // 拦截的方法名称。当前正在执行的方法
//        String methodName = pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName(); 
//        logBean.setMethod(methodName);
//        
//        // 设置ip
//        logBean.setIp(getRemoteIp());
//        
//        HttpSession session = (HttpSession)MythreadLocal.get("session");
//		ExUserBean user = (ExUserBean) session.getAttribute("user");
//		if (user ==null) {
//			user = new ExUserBean();
//			user.setAge("26");
//			user.setName("宋斌");
//			user.setDeptCode("2002");
//			user.setRole(0);
//			user.setCode("108");
//		}
//		
//		logBean.setUserCode(user.getCode());
//		logBean.setUsername(user.getName());
//		ObjectMapper mapper = new ObjectMapper(); //转换器 
//		String json=mapper.writeValueAsString(logBean); //将对象转换成json
//		logger.info("执行开始:" + json);
//    }
//    
//
//    /**
//     * 
//     * @param joinPoint
//     * @throws JsonProcessingException 
//     */
//    private void after(JoinPoint pjp) throws JsonProcessingException{
//       
//        LogBean  logBean = new LogBean(); 
//        List list = new ArrayList();
//        // 设置参数
//        for (int i=0;i<pjp.getArgs().length;i++) {
//        	
//        	if (!(pjp.getArgs()[i] instanceof HttpServletRequest 
//        			|| pjp.getArgs()[i] instanceof HttpServletResponse 
//        			|| pjp.getArgs()[i] instanceof CommonsMultipartFile
//        			|| pjp.getArgs()[i] instanceof BeanPropertyBindingResult))  
//        	{
//        		list.add(pjp.getArgs()[i]);
//        	}
//        	
//        }
//        logBean.setArgs(list);
//        MethodSignature ms=(MethodSignature) pjp.getSignature();
//        Method method=ms.getMethod();
//        
//        if (method.isAnnotationPresent(Log.class)) {
//        	String log = method.getAnnotation(Log.class).value();
//            logBean.setContent(log);
//       }
//        // 拦截的方法名称。当前正在执行的方法
//        String methodName = pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName(); 
//        logBean.setMethod(methodName);
//        
//        // 设置ip
//        logBean.setIp(getRemoteIp());
//        
//        HttpSession session = (HttpSession)MythreadLocal.get("session");
//		ExUserBean user = (ExUserBean) session.getAttribute("user");
//		if (user ==null) {
//			user = new ExUserBean();
//			user.setAge("26");
//			user.setName("宋斌");
//			user.setDeptCode("2002");
//			user.setRole(0);
//			user.setCode("108");
//		}
//		
//		logBean.setUserCode(user.getCode());
//		logBean.setUsername(user.getName());
//		ObjectMapper mapper = new ObjectMapper(); //转换器 
//		String json=mapper.writeValueAsString(logBean); //将对象转换成json
//		logger.info("执行开始:" + json);
//
//        String time1 =  "" + (System.currentTimeMillis()-time.get());
//        logBean.setResponseTime(time1);       
//        
//    }
//     
//    /**
//     * 获取远程ip
//     * @return
//     */
//    private String getRemoteIp() {  
//        String ipAddress = null;  
//        HttpServletRequest request =getCurrentRequest();
//        if (null == request) {  
//            return "0.0.0.1";  
//        }  
//        ipAddress = request.getHeader("x-forwarded-for");  
//        if (ipAddress == null || ipAddress.length() == 0  
//                || "unknown".equalsIgnoreCase(ipAddress)) {  
//            ipAddress = request.getHeader("Proxy-Client-IP");  
//        }  
//        if (ipAddress == null || ipAddress.length() == 0  
//                || "unknown".equalsIgnoreCase(ipAddress)) {  
//            ipAddress = request.getHeader("WL-Proxy-Client-IP");  
//        }  
//        if (ipAddress == null || ipAddress.length() == 0  
//                || "unknown".equalsIgnoreCase(ipAddress)) {  
//            ipAddress = request.getRemoteAddr();  
//            // 这里主要是获取本机的ip,可有可无  
//            if (ipAddress.equals("127.0.0.1")  
//                    || ipAddress.endsWith("0:0:0:0:0:0:1")) {  
//                // 根据网卡取本机配置的IP  
//                InetAddress inet = null;  
//                try {  
//                    inet = InetAddress.getLocalHost();  
//                } catch (UnknownHostException e) {  
//                    e.printStackTrace();  
//                }  
//                ipAddress = inet.getHostAddress();  
//            }  
//        }  
//        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
//        if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()  
//                                                            // = 15  
//            if (ipAddress.indexOf(",") > 0) {  
//                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));  
//            }  
//        }  
//        // 或者这样也行,对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
//        // return  
//        // ipAddress!=null&&!"".equals(ipAddress)?ipAddress.split(",")[0]:null;  
//        return ipAddress;  
//    }
//    
//   
//    
//   private  HttpServletRequest getCurrentRequest() {
//	   return (HttpServletRequest)MythreadLocal.get("request");
//   }
//}